package com.test.baseGroovy

/**
 * IOTest--GroovyTest
 *
 * @author: Jovi
 *
 * @createTime: 2018-06-13 10:25
 *
 * */

/*===================================文本文件读=====================================*/
def filePath = IOTest.getClassLoader().getResource("test.txt").getPath()
//def filePath = "/Users/Jovi/Documents/JOVI/code/GroovyTest/src/main/resources/test3.txt"
println filePath
def file = new File(filePath)

file.eachLine("utf-8") { //指定编码格式
    println it
}

IOTest.getClassLoader().getResource("test.txt").eachLine {
    println it
}

println IOTest.getClassLoader().getResource("test.txt").text

/*===================================文本文件写=====================================*/
def filePath1 = "/Users/Jovi/Documents/JOVI/code/GroovyTest/src/main/resources"

def file1 = new File(filePath1+"/test1.txt")
file1.withPrintWriter {
    it.println("测试")
    it.println("hello world")
}

def writer = file1.newPrintWriter()
writer.println("测试！")
writer.println("hello world")
writer.flush()
writer.close()


/*===================================文件夹操作=====================================*/
def filePath2 = "/Users/Jovi/Documents/JOVI/code/GroovyTest/src/main"
def package1 = new File(filePath2)
package1.eachDir {
    println "文件夹："+it.name
}

package1.eachFile {
    println "文件："+it.name
}

package1.eachDirRecurse { //深度遍历文件夹
    println "文件夹："+it.name
}

package1.eachFileRecurse { //深度遍历文件
    println "文件："+it.path
}

def filePath3 = IOTest.getClassLoader().getResource("test.txt").getPath()
println filePath3

def file2 = new File(filePath3)
file2.withInputStream {
    ism -> ism.eachLine {
        println it
    }
}